package cn.signalpha.modules.shop.service;

import cn.signalpha.modules.shop.entity.SellerAddress;
import cn.signalpha.modules.shop.mapper.SellerAddressMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import static cn.signalpha.common.cache.CacheNames.SELLER_ADDRESS;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class SellerAddressService extends ServiceImpl<SellerAddressMapper, SellerAddress> {

	@Cacheable(cacheNames = SELLER_ADDRESS, key = "#id", unless="#result==null")
	public SellerAddress getById(Long id) {
		return getOne(Wrappers.<SellerAddress>lambdaQuery().eq(SellerAddress::getId, id));
	}

	@CacheEvict(cacheNames = SELLER_ADDRESS, allEntries = true)
	public void removeCaCheById(Long id) {
	}

	/**
	 * 清除商户默认地址
	 */
	public void cleanUserDefaultAddress(Long sellerId) {
		update(SellerAddress.builder().isDefault(0).build(), Wrappers.<SellerAddress>lambdaQuery().eq(SellerAddress::getSellerId, sellerId).eq(SellerAddress::getIsDefault, 1));
	}

}

